<!DOCTYPE HTML>

<?php 
	session_start(); 
	
	if(isset($_SESSION['id'], $_SESSION['vorname'], $_SESSION['name'])){
		$id = $_SESSION['id'];
		$vorname = $_SESSION['vorname'];
		$name = $_SESSION['name'];
	}

?>
<html>

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<link href='./images/favicon.ico' rel='shortcut icon' type='image/png'>
<link rel="stylesheet" type="text/css" href="./css/index.css">
<title>Mecha</title>
</head>


<body id="fondo">
<div id="cuerpo">
<fieldset style="width: 1000px; color: #FFF;";>
    <?php
        include 'header.php';
    ?>
<div id="subcuerpo">
	<?php if($_SESSION['workstation']>5) : ?>
		<a href="add_mecha.php"><h2>Add new mecha unit</h2></a>
		<div id="cambios" style="visibility:hidden; color:red">New mecha added</div>
	<?php endif; ?>

    <table id='mecha_list'>
    	<tr>
    		<td><h2>Mecha units</h2></td>
    	<?php 
    		$link = mysqli_connect('localhost', 'root', '');
			if (mysqli_connect_errno()) {
				echo 'Cannot connect to mysql';
			exit;
			}
	
			$sql = "USE web_db";
			$resultado = mysqli_query($link,$sql);
	
			$sql = "SELECT * FROM mecha";
			$resultado = mysqli_query($link,$sql);
			if(!$resultado){
				echo "incorrect id";
			exit;
			}
			
			while ($fila = mysqli_fetch_row($resultado)) {
    			echo "<tr>";
    			echo "<td><h3>Identifier: $fila[0]</h3></td>";
    			echo "<tr><td><table><tr><td>";
    				echo "<img width='120' height='150' src='$fila[1]'>";
    				echo "<td><h4>Parts:</h4></td><td>";
    				PartList($fila[0]);
    				echo "</td>";
    				echo "<td><h4>Missions:</h4></td><td>";
    				MissionList($fila[0]);
    				echo "</td>";
    				echo "<td>";
    					echo "<form method='POST' action='erase_mec.php'>";
    					echo "<input type='hidden' name='erase_id' value='$fila[0]'/>";
    					echo "<input type='submit' value='Delete'></input>";
    					echo "</form>";
    				echo "</td>";
    			echo "</td></tr>";
    			echo "<tr><td>Price fo the Mecha-Unit:</td><td>";
    			echo Price($fila[0]);
    			echo "</td></tr>";
    			echo "</table>";
    			echo "<hr></td></tr>";
    			echo "</tr>";
    		}

    	?>
    	</tr>

    </table>

    <table>
    	<tr>
    		<td>Total Price of Mecha Units:</td><td><?php TotalPrice()?></td>
    	</tr>
	</table>
</div>




	<?php
		include 'footer.php';
	?>
</fieldset>
</div>
</body>
<script type="text/javascript">
	var cambio = "<?php
						if(isset($_SESSION['new_mecha']) && $_SESSION['new_mecha']=="si"){
							$_SESSION['new_mecha'] = 'no';
							echo 'true';
						}else{
							echo 'false';
						}
					?>";

	if(cambio == "true"){
		document.getElementById("cambios").style.visibility="visible";
	}
</script>

<?php 
function TotalPrice() {
	$sql = "USE web_db";
    $link = mysqli_connect('localhost', 'root', '');
    $result = mysqli_query($link,$sql);
    
    $sql = "SELECT SUM(p.price)
			FROM part p, mech_part mp
			WHERE p.sn=mp.sn";

    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_row($result);

    echo "$row[0]€";
}
function Price($id) {
	$sql = "USE web_db";
    $link = mysqli_connect('localhost', 'root', '');
    $result = mysqli_query($link,$sql);
    
    $sql = "SELECT SUM(p.price)
			FROM part p, mech_part x, mecha m
			WHERE p.sn=x.sn AND x.mec_id=m.mec_id AND m.mec_id='$id'";
    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_row($result);

    echo "$row[0]€";
	
}

function PartList($mec_id) {
	$sql = "USE web_db";
	$link = mysqli_connect('localhost', 'root', '');
	$result = mysqli_query($link,$sql);
	
	$sql = "SELECT p.kind, mp.sn, p.name 
			FROM mech_part mp, part p 
			WHERE mp.mec_id = '$mec_id' AND mp.sn = p.sn
			ORDER BY p.kind";
	$result = mysqli_query($link,$sql);

	echo "<table>";
	while ($row = mysqli_fetch_row($result)) {
    echo "<tr>";
    echo "<td align='left'>";
    echo "$row[0]:";
    echo "<td align='left'>";
    echo "<select>";
	echo "<option>$row[1] - $row[2]</option>";
	echo "</td>";
    echo "</td>";
    echo "</tr>";
	}
	echo "</table>";
}



function MissionList($mec_id) {
	$sql = "USE web_db";
	$link = mysqli_connect('localhost', 'root', '');
	$result = mysqli_query($link,$sql);

	$sql = "SELECT m.miss_nr, m.name 
			FROM mission m, mech_mission mm 
			WHERE mm.mec_id = '$mec_id' AND mm.mission_nr = m.miss_nr";
/*
	$sql = "SELECT * FROM mech_mission WHERE mec_id='$mec_id'";
*/	
	$result = mysqli_query($link,$sql);

	//Con el aux se le añade un punto al final, para que el echo no acabe en una coma
	$aux = "";

	while ($row = mysqli_fetch_row($result)) {
	$aux = $aux . "$row[0]-$row[1], ";
	}
	$aux = substr($aux, 0, -2);
	$aux = $aux . ".";
	echo $aux;
}
?>